import requests,urllib
import time
import json
import sys
from tkinter import messagebox
if __name__ == "__main__":
    if len(sys.argv) > 1:
        
        dic =  dict({"buildType":"ios" if sys.argv[1].endswith("ipa") else 'android',"_api_key":'38dbfa3eedfaf924ffdfd45132822091'})
        try:
            print("等待上传")
            response = requests.post("https://www.pgyer.com/apiv2/app/getCOSToken",data = dic)
            responseData = json.loads(response.text)["data"]
            dic2 = responseData["params"]
            print("正在上传")
            response2 = requests.post(responseData["endpoint"],data = dic2, files={'file': open(sys.argv[1], 'rb')})
            print("上传完成")
            print(time.strftime("%Y-%m-%d %H:%M:%S"))
            print(f"https://www.pgyer.com/apiv2/app/buildInfo?_api_key=38dbfa3eedfaf924ffdfd45132822091&buildKey={dic2['key']}")
            print("等待蒲公英组织数据20s")
            time.sleep(20)
            print(time.strftime("%Y-%m-%d %H:%M:%S"))
            response3 = requests.get("https://www.pgyer.com/apiv2/app/buildInfo",params = {"_api_key":"38dbfa3eedfaf924ffdfd45132822091","buildKey":dic2["key"]})
            

            messagebox.showinfo(title="上传完成",message=f"https://www.pgyer.com/{json.loads(response3.text)['data']['buildShortcutUrl']}") 
        except urllib.error.URLError as e:
            print('TIME OUT')
    else:
        print("无文件")